package com.hscm.boss.mvc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hscm.boss.common.enums.PublicStatus;
import com.hscm.boss.common.enums.ResponseCode;
import com.hscm.boss.common.exception.BossException;
import com.hscm.boss.common.utils.NoUtil;
import com.hscm.boss.dao.entity.UserAccount;
import com.hscm.boss.dao.entity.UserInfo;
import com.hscm.boss.dao.mapper.UserAccountMapper;
import com.hscm.boss.dao.mapper.UserInfoMapper;
import com.hscm.boss.mvc.model.from.user.UserEditFrom;
import com.hscm.boss.mvc.service.UserService;
import jakarta.annotation.Resource;
import org.apache.catalina.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class UserServiceImpl implements UserService {


    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private UserAccountMapper userAccountMapper;

    @Override
    public IPage<UserInfo> pageList(Integer pageNo, Integer pageSize) {
        //对数据进行排序
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");

        return userInfoMapper.selectPage(Page.of(pageNo, pageSize), wrapper);
    }


    @Transactional(rollbackFor = Exception.class) //下面是两个表操纵因此要加事务
    // 如果执行过程中发生异常，则会回滚事务。
    @Override //synchronized 的意义是查出这个用户是唯一的 之后的添加账户才能添加
    //添加操作不是一个高频操作  因此对性能不会造成太大影响
    public synchronized void editUser(UserEditFrom userEditFrom) {
        if (StrUtil.isNotBlank(userEditFrom.getId())) {//修改功能
            UserInfo edit = new UserInfo();
            edit.setUserEmail(userEditFrom.getUserEmail());
            edit.setPhone(userEditFrom.getPhone());
            edit.setShopName(userEditFrom.getShopName());
            edit.setShopScope(userEditFrom.getShopScope());

            edit.setId(userEditFrom.getId());

            userInfoMapper.updateById(edit);

        } else {//新增功能  登录名称不能重复必须唯一

            QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("login_name", userEditFrom.getLoginName());
            UserInfo userInfo = userInfoMapper.selectOne(wrapper);


            if (userInfo != null) {
                //登录名称已经使用
                throw new BossException(ResponseCode.USER_LOGIN_NAME_EXISTS);
            }

            //不重复就创建新的商户
            userInfo = new UserInfo();
            userInfo.setId(NoUtil.getUUID());
            userInfo.setUserNo(NoUtil.getUserNo());
            userInfo.setLoginName(userEditFrom.getLoginName());
            userInfo.setAccountNo(NoUtil.getAccount());
            userInfo.setPhone(userEditFrom.getPhone());
            userInfo.setPassword(NoUtil.getUUID());
            userInfo.setPayPwd(NoUtil.getUUID());
            userInfo.setShopName(userEditFrom.getShopName());
            userInfo.setShopScope(userEditFrom.getShopScope());
            userInfo.setUserEmail(userEditFrom.getUserEmail());
            userInfo.setStatus(PublicStatus.DISABLE.name());
            userInfo.setCreateTime(new Date());
            userInfo.setRemark("创建商户");

            userInfoMapper.insert(userInfo);

            //创建资金账号
            UserAccount account = new UserAccount();
            account.setId(NoUtil.getUUID());
            account.setUserNo(userInfo.getUserNo());
            account.setAccountNo(userInfo.getAccountNo());
            account.setBalance(new BigDecimal("0"));
            account.setTotalIncome(new BigDecimal("0"));
            account.setTotalExpend(new BigDecimal("0"));
            account.setStatus(PublicStatus.DISABLE.name());
            account.setRemark("新用户创建资金账户");
            account.setEditTime(new Date());

            userAccountMapper.insert(account);

        }
    }

    @Override
    public UserInfo queryById(String userId) {
        return userInfoMapper.selectById(userId);
    }

    @Override
    public void editStatus(String userNo) {
        //寻找用户
        UserInfo userInfo = userInfoMapper.selectOne(new QueryWrapper<UserInfo>()
                .eq("user_no", userNo)
        );

        if (userInfo == null) {
            throw new BossException(ResponseCode.USER_NOT_EXISTS);
        }

        //获取当亲商户的状态值

        //默认是ENABLE
        String editStatus = PublicStatus.ENABLE.name();

        String status = userInfo.getStatus();
        if (PublicStatus.ENABLE.name().equals(status)) {
            editStatus = PublicStatus.DISABLE.name();
        }

        UpdateWrapper<UserInfo> updateWrapper = new UpdateWrapper<UserInfo>();
        updateWrapper.eq("id", userInfo.getId())
                .set("status", editStatus);
        userInfoMapper.update(updateWrapper);
    }
}
